#import "ALVersionsFilterViewController.h"


#pragma mark Constants


#pragma mark -
@interface ALVersionsFilterViewController ()
-(NSString *)currentVersionsFilterKey;
-(void)updateDateFieldsEnabledStateWithFilterMode:(NSInteger)filterMode;
@end


#pragma mark -
@implementation ALVersionsFilterViewController

@synthesize versionsBeginDateField;
@synthesize versionsEndDateField;
@synthesize versionsModeMatrix;


-(void)updateDateFieldsEnabledStateWithFilterMode:(NSInteger)filterMode {
    BOOL dateModeIsActive = filterMode == ALVersionsFilterModeDate;
    [[self versionsEndDateField] setEnabled:dateModeIsActive];
    [[self versionsBeginDateField] setEnabled:dateModeIsActive];
}


#pragma mark -
#pragma mark Accessors

-(NSString *)currentVersionsFilterKey {
    ALDetailMode detailMode = (ALDetailMode)[[NSUserDefaults standardUserDefaults] integerForKey:ALModeSelection];
    return [NSString stringWithFormat:@"%@%ld", kALVersionsFilterKey, detailMode];
}


#pragma mark -
#pragma mark Actions

-(IBAction)changeVersionsFilterMode:(id)sender {
    NSCell *selected = [sender selectedCell];
    NSInteger tag = [selected tag];
    NSString *filterKey = [self currentVersionsFilterKey];
    [[NSUserDefaults standardUserDefaults] setInteger:tag forKey:filterKey];
    [self updateDateFieldsEnabledStateWithFilterMode:tag];
}


#pragma mark -
#pragma mark Popover delegate

-(void)popoverWillShow:(NSNotification *)notification {
    NSInteger filterMode = [[NSUserDefaults standardUserDefaults] integerForKey:[self currentVersionsFilterKey]];
    [[self versionsModeMatrix] selectCellWithTag:filterMode];
    [self updateDateFieldsEnabledStateWithFilterMode:filterMode];
}

@end
